package com.sdwulongshan.register.reset.register;

import com.sdwulongshan.register.reset.register.ex.ParamterIsNullException;
import com.sdwulongshan.register.reset.register.tool.AESTool;
/**
 * 注册器实体类<br>
 * 类是final的，不希望被继承，构造器是私有的，不希望被new对象
 * @author ForMat
 * @version JDK_1.8.0_221
 */
public final class Register {
	/**
	 * 此方法用于生成注册码
	 * @param androidId 用户的AndroidId
	 * @param key 密钥
	 * @return 注册码
	 * @author ForMat
	 * @throws com.sdwulongshan.register.reset.register.ex.ParamterIsNullException 当传入的参数为null或空串将会抛出此异常
	 */
	public static String createRegistercdoe(String androidId,String key) {
		if("".equals(key) || key == null || "".equals(androidId)|| null == androidId) {
			throw new ParamterIsNullException("缺少必要参数！");
		} else {
			byte[] aes = AESTool.encodeing(androidId, key);
			String code = parseByte2HexStr(aes);
			return code;
		}

	}

	public static boolean checkRegistercode(String androidId,String key,String registercode) {
		if("".equals(androidId) || androidId == null || "" .equals(key) || key == null || "".equals(registercode) || registercode == null ) {
			throw new ParamterIsNullException("缺少必要参数！");
		} else if(createRegistercdoe(androidId, key).equals(registercode)) {
			return true;
		}else {
			return false;
		}
	}

	/**
	 * 此方法用于把AES数组转成字符串
	 * @param aes AES数组
	 * @return AES字符串
	 */
	public static String parseByte2HexStr(byte aes[]) {

		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < aes.length; i++) {

			String hex = Integer.toHexString(aes[i] & 0xFF);

			if (hex.length() == 1) {

				hex = '0' + hex;

			}

			sb.append(hex.toUpperCase());

		}

		return sb.toString();

	}

	private Register() {
		// NOOOP
	}

}
